// 城市区域管理 - 城市层级结构接口
const Router = require("koa-router");
const router = new Router();

// 城市层级数据结构（与参考代码相同）
const cityList = [
  {
    label: "北京总部",
    children: [
      {
        label: "东城区",
        children: [
          { label: "东城区充电站01" },
          { label: "东城区充电站02" },
          { label: "东城区充电站03" },
          { label: "东城区充电站04" },
        ],
      },
      {
        label: "西城区",
        children: [
          { label: "西城区充电站01" },
          { label: "西城区充电站02" },
          { label: "西城区充电站03" },
        ],
      },
      {
        label: "朝阳区",
        children: [
          { label: "朝阳区充电站01" },
          { label: "朝阳区充电站02" },
          { label: "朝阳区充电站03" },
        ],
      },
      {
        label: "海淀区",
        children: [{ label: "海淀区充电站01" }, { label: "海淀区充电站02" }],
      },
      {
        label: "丰台区",
        children: [{ label: "丰台区充电站01" }, { label: "丰台区充电站02" }],
      },
    ],
  },
  {
    label: "深圳总部",
    children: [
      {
        label: "南山区",
        children: [
          {
            label: "南山区充电站01",
          },
        ],
      },
      {
        label: "福田区",
        children: [
          {
            label: "福田区充电站01",
          },
        ],
      },
    ],
  },
  {
    label: "青岛市",
    children: [
      {
        label: "市南区",
        children: [
          {
            label: "市南区充电站01",
          },
          {
            label: "市南区充电站02",
          },
        ],
      },
      {
        label: "市北区",
        children: [
          {
            label: "市北区充电站01",
          },
          {
            label: "市北区充电站02",
          },
        ],
      },
      {
        label: "崂山区",
        children: [
          {
            label: "崂山区充电站01",
          },
          {
            label: "崂山区充电站02",
          },
        ],
      },
    ],
  },
  {
    label: "武汉市",
    children: [
      {
        label: "武汉充电站01",
      },
      {
        label: "武汉充电站02",
      },
      {
        label: "武汉充电站03",
      },
    ],
  },
  {
    label: "成都市",
    children: [
      {
        label: "成都充电站01",
      },
      {
        label: "成都充电站02",
      },
      {
        label: "成都充电站03",
      },
    ],
  },
  {
    label: "上海市",
    children: [
      {
        label: "上海充电站01",
      },
      {
        label: "上海充电站02",
      },
      {
        label: "上海充电站03",
      },
    ],
  },
  {
    label: "长沙市",
    children: [
      {
        label: "长沙充电站01",
      },
      {
        label: "长沙充电站02",
      },
      {
        label: "长沙充电站03",
      },
    ],
  },
];

// 创建数据备份（用于重置，如果需要的话）
const originalCityList = JSON.parse(JSON.stringify(cityList));

/**
 * 城市层级结构接口
 * 路径：GET /city/cityList
 *
 * 说明：此接口不需要前端传递任何参数，直接返回完整的城市层级数据
 * 用于计费管理等需要城市层级选择功能的模块
 */
router.get("/Charging/city", (ctx) => {
  try {
    console.log("城市列表接口被调用，返回完整层级数据");

    // 返回完整数据
    ctx.body = {
      code: 200,
      success: true,
      message: "操作成功",
      data: cityList,
    };
  } catch (error) {
    console.error("城市列表接口出错:", error);
    ctx.body = {
      code: 500,
      success: false,
      message: `服务器内部错误: ${error.message}`,
      data: null,
    };
  }
});

/**
 * 根据城市名称获取详情接口
 * 路径：POST /city/cityDetail
 *
 * 请求参数：
 * {
 *   cityName: "北京总部", // 城市/区域名称
 * }
 */
router.post("/city/cityDetail", (ctx) => {
  try {
    // 解析请求参数
    const requestBody = ctx.request.body ? JSON.parse(ctx.request.body) : {};
    const { cityName } = requestBody;

    console.log("城市详情接口收到参数:", { cityName });

    if (!cityName) {
      ctx.body = {
        code: 400,
        success: false,
        message: "缺少必要的参数: cityName",
        data: null,
      };
      return;
    }

    // 递归查找城市/区域
    function findCity(node, name) {
      if (node.label === name) {
        return node;
      }

      if (node.children) {
        for (let child of node.children) {
          const found = findCity(child, name);
          if (found) return found;
        }
      }

      return null;
    }

    // 在所有城市中查找
    let cityDetail = null;
    for (let city of cityList) {
      cityDetail = findCity(city, cityName);
      if (cityDetail) break;
    }

    if (!cityDetail) {
      ctx.body = {
        code: 404,
        success: false,
        message: `未找到名称为 ${cityName} 的城市或区域`,
        data: null,
      };
      return;
    }

    // 返回响应
    ctx.body = {
      code: 200,
      success: true,
      message: "操作成功",
      data: cityDetail,
    };
  } catch (error) {
    console.error("城市详情接口出错:", error);
    ctx.body = {
      code: 500,
      success: false,
      message: `服务器内部错误: ${error.message}`,
      data: null,
    };
  }
});

// 导出路由
module.exports = router;
